package collect;

public class ReorderDemo {

    private int count = 2;
    private boolean flag = false;

    public void write1()  {
        try {
            Thread.sleep(1);
        } catch (InterruptedException e) {
            // ignore
        }
        count = 10;
        flag = true;
    }

    public void read1()  {
        if (flag) {
            // 因为重排序可能输出2
            System.out.println(count);
        }
    }

    public static void main(String[] args) throws InterruptedException {
        for (int i=0; i < 300; i++) {
            ReorderDemo reorderDemo = new ReorderDemo();
            Thread t1 = new Thread(reorderDemo::write1);
            Thread t2 = new Thread(reorderDemo::read1);
            t2.start();
            t1.start();
        }
        Thread.sleep(1000);
    }
}
